﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WJP.Dependency
{
    /// <summary>
    /// 此接口用于包装从IOC容器解析的对象
    /// <para>它继承自<see cref="IDisposable"/>，所以被解析的对象很容易被释放</para>
    /// <para>这是非通用版本的<see cref="IDisposableDependencyObjectWrapper{T}"/>接口</para>
    /// </summary>
    internal class DisposableDependencyObjectWrapper : DisposableDependencyObjectWrapper<object>, IDisposableDependencyObjectWrapper
    {
        public DisposableDependencyObjectWrapper(IIocResolver iocResolver, object obj)
            : base(iocResolver, obj)
        {

        }
    }

    /// <summary>
    /// 用于包装从IOC容器解析的对象
    /// <para>它继承自<see cref="IDisposable"/>，所以被解析的对象很容易被释放</para>
    /// </summary>
    internal class DisposableDependencyObjectWrapper<T> : IDisposableDependencyObjectWrapper<T>
    {
        private readonly IIocResolver _iocResolver;

        public T Object { get; private set; }

        public DisposableDependencyObjectWrapper(IIocResolver iocResolver, T obj)
        {
            _iocResolver = iocResolver;
            Object = obj;
        }

        public void Dispose()
        {
            _iocResolver.Release(Object);
        }
    }
}
